//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
namespace DNAide.Drawing
{
	
	public static partial class BitmapAide
	{
		
		/// <summary>
		/// Crops a bitmap
		/// </summary>
		/// <param name="bitmapToCrop"></param>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <param name="width"></param>
		/// <param name="height"></param>
		public static void Crop(ref Bitmap bitmapToCrop, Int32 x, Int32 y, Int32 width, Int32 height)
		{
			Crop(ref bitmapToCrop, x, y, width, height, null);
		}

		
		/// <summary>
		/// Crops a bitmap
		/// </summary>
		/// <param name="bitmapToCrop"></param>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <param name="width"></param>
		/// <param name="height"></param>
		/// <param name="graphicsSettings"></param>
		public static void Crop(ref Bitmap bitmapToCrop, Int32 x, Int32 y, Int32 width, Int32 height, GraphicsSettings graphicsSettings)
		{
			Crop(ref bitmapToCrop, new Rectangle(x, y, width, height), graphicsSettings);
		}


		/// <summary>
		/// Crops a bitmap
		/// </summary>
		/// <param name="bitmapToCrop"></param>
		/// <param name="rectangle"></param>
		public static void Crop(ref Bitmap bitmapToCrop, Rectangle rectangle)
		{
			Crop(ref bitmapToCrop, rectangle, null);
		}

		
		/// <summary>
		/// Crops a bitmap
		/// </summary>
		/// <param name="bitmapToCrop"></param>
		/// <param name="rectangle"></param>
		/// <param name="graphicsSettings"></param>
		public static void Crop(ref Bitmap bitmapToCrop, Rectangle rectangle, GraphicsSettings graphicsSettings)
		{
			Bitmap croppedBitmap = new Bitmap(rectangle.Width, rectangle.Height);
			using (Graphics g = Graphics.FromImage((Image)croppedBitmap))
			{
				if (graphicsSettings != null)
				{
					graphicsSettings.ApplySettings(g);
				}
				g.DrawImage(bitmapToCrop, 0, 0, rectangle, GraphicsUnit.Pixel);
			}
			bitmapToCrop.Dispose(); bitmapToCrop = null;
			bitmapToCrop = BitmapAide.CopyBitmap(croppedBitmap);
			croppedBitmap.Dispose(); croppedBitmap = null;
		}

	}

}
